/**
 * @description utils api 路由
 */
const path = require("path");
const router = require("koa-router")();
const { loginCheck, loginRedirect } = require("../../middlewares/loginChecks");
const multer = require("@koa/multer");
const { saveFile } = require("../../controller/utils");

// 存储目录 当前目录向上两层和src同级下的uploadfiles文件夹下
const DIST_FOLDER_PATH = path.join(__dirname, "..", "..", "..", "uploadfiles");
const upload = multer({ dest: DIST_FOLDER_PATH });

router.prefix("/api/utils");

// 上传图片
router.post("/upload", loginCheck, upload.single("file"), async (ctx, next) => {
  const file = ctx.request.file;
  console.log(file);
  const { size, mimetype, originalname, path } = file;
  // 调用controller
  ctx.body = await saveFile({
    size,
    mimetype,
    originalname,
    path
  });
});

module.exports = router;